<template>
	<view class="content">
		<view class="headBox">
			<view class="title">授权登录</view>
			<image class="logoImg" src="../../static/img/logo2.png"></image>
			<view class="title-2">吉东回收</view>
			<view class="cont-box">
				<view class="ipt-box">
					<uni-data-select class="data-select-box" :clear="false" v-model="tenantId" :localdata="stateList"
						placeholder="请选择租户"></uni-data-select>
				</view>
				<view class="ipt-box">
					<text class="jia">+86</text>
					<!-- #ifndef APP-NVUE -->
					<u-input placeholder="请输入手机号" type="number" border="none" v-model="phone">
					<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						<u--input placeholder="请输入手机号" type="number" border="none" v-model="phone">
						<!-- #endif -->
							<template slot="suffix">
								<u-code class="code-btn" ref="uCode" @change="codeChange" seconds="60"
									changeText="X秒重新获取"></u-code>
								<u-button class="code-btn" @tap="getCode" :text="tips" type="success"
									size="mini"></u-button>
							</template>
					<!-- #ifndef APP-NVUE -->
					</u-input>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					</u--input>
					<!-- #endif -->
				</view>
				<view class="ipt-box">
					<u--input placeholder="请输入验证码" border="none" v-model="code"></u--input>
				</view>


			</view>
			<view class="btn-box">
				<button class="login-button login-button-wx" @click="bindLogin">登录</button>
			</view>
			<!-- <view class="form-item-gx">
				<u-checkbox-group>
					<u-checkbox size="26rpx" :customStyle="{marginBottom: '2px',}" activeColor="#15A7F4" iconSize="14"
						labelColor="#000" labelSize="12px" @change="checkedChange" :checked="gxChecked"
						label="我已阅读并同意遵守" name="11">
					</u-checkbox>
				</u-checkbox-group>
				<text class="zpxz" @click="toAgreement('user_agreement')">《服务协议》</text>
				<text class="zpxz" @click="toAgreement('user_agreement')">《隐私协议》</text>
			</view> -->
		</view>


	</view>
</template>

<script>
	import {
		sendVerificationCode,
		login,
		downTenant,
		getInfo
	} from '@/api/user'
	export default {
		data() {
			return {
				title: '登录',
				gxChecked: false,
				userInfo: {},
				isLogin: true,
				tips: '',
				code: '',
				phone: '',
				tenantId: '',
				stateList: [],

			}
		},
		onLoad() {},
		onShow() {
			this.init()
			if (uni.getStorageSync('userToken')) {

				if (uni.getStorageSync('subType') == 1001004) {
					uni.setStorageSync('tabUserVal', 0)
					uni.reLaunch({
						url: "/pages/dispatch/index" // 派送司机
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					});
				}

			}
		},
		watch: {
			value(newValue, oldValue) {
				// console.log('v-model', newValue);
			}
		},

		methods: {
			async init() {
				let res = await downTenant()
				if (res.code == 200) {
					let list = res.data

					list.forEach(item => {
						item.text = item.entryKey
						item.value = item.tenantId
					})
					this.stateList = list
				}

			},
			bindLogin() {
				let data = {
					username: this.phone,
					code: this.code,
					tenantId: this.tenantId
				}
				if (!data.tenantId) {
					uni.$u.toast(`请选择租户`)
					return
				}
				if (!data.code) {
					uni.$u.toast(`请输入验证码`)
					return
				}
				console.log(data)
				login(data).then(res => {
					console.log(res)
					if (res.code == 200) {
						uni.setStorageSync('userToken', res.token)

						getInfo().then(result => {
							if(result.code==200){
								uni.setStorageSync('userInfo', JSON.stringify(result.user))
								let subTypes = result.user.typeCodes.split(',')
								console.log(subTypes)
								uni.setStorageSync('subType', subTypes[0])
								if (subTypes[0] == 1119999) {
									setTimeout(() => {
										uni.reLaunch({
											url: "/pages/dispatch/user" // 派送司机
										})
									}, 1000)
								
								} else {
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/user/index'
										});
									}, 1000)
								}
							}
							
						})






					}
				})


			},
			// 勾选
			checkedChange(e) {
				this.gxChecked = e
				console.log(e)
			},
			// 跳协议
			toAgreement(type) {
				uni.navigateTo({
					url: `/packageExpert/user/agreement?type=${type}`
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				console.log(this.phone)
				let phone = this.phone
				let phoneReg = /^1[3456789]\d{9}$/; // 
				if (!phoneReg.test(phone)) {
					uni.$u.toast('手机号码格式不正确');
					return false
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						sendVerificationCode({
							phone: phone
						}).then(res => {
							console.log(res)
							uni.$u.toast('验证码已发送');
						}).catch(err => {
							console.log(err)
						})

						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			},

		}
	}
</script>

<style lang="less" scoped>
	.content {
		background: #fff;
		font-family: PingFang SC, PingFang SC;
		height: 100vh;
		overflow-y: auto;

		.headBox {
			padding-top: 102rpx;
			// background-image: linear-gradient(to bottom, #15A7F4, #fff);
			background: linear-gradient(180deg, #2DD1AC 25%, rgba(45, 209, 172, 0) 100%);
			padding-bottom: 92rpx;

			.title {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 34rpx;
				color: #FFFFFF;
				text-align: center;
			}

			.logoImg {
				width: 140rpx;
				height: 140rpx;
				background: #FFFFFF;
				border-radius: 50%;
				display: block;
				margin: 0 auto;
				margin-top: 42rpx;
			}

			.title-2 {
				font-family: PingFang SC, PingFang SC;
				font-weight: 600;
				font-size: 44rpx;
				color: #FFFFFF;
				text-align: center;
				margin-top: 20rpx;
				margin-bottom: 100rpx;
			}

		}

		.cont-box {
			padding: 0 75rpx;

			.code-btn {
				background: none;
				color: #2DD1AC !important;
			}

			.ipt-box {
				height: 88rpx;
				display: flex;
				align-items: center;
				padding: 0 30rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				border: 2rpx solid #eee;
				margin-bottom: 30rpx;

				.jia {
					font-weight: bold;
					font-size: 28rpx;
					color: #222222;
					margin-right: 20rpx;
				}

				::v-deep .data-select-box {
					width: 100%;
					height: 100%;

					.uni-stat__select {
						height: 100%;
					}

					.uni-select {
						border: none;
						padding: 0;
					}

					.uni-select__input-placeholder {
						font-weight: 400;
						font-size: 28rpx;
						color: #999999;
					}

					.uni-stat-box {
						background: none;
					}

					.uni-select--disabled {
						background: none;
					}
				}
			}


			::v-deep .u-button--success {
				color: #2DD1AC !important;
				border: none;
				background: none;
			}
		}

		.form-item-gx {
			margin-top: 240rpx;
			display: flex;
			font-weight: 400;
			font-size: 24rpx;
			color: #15A7F4;
			justify-content: center;
			position: fixed;
			bottom: 64rpx;
			left: 0;
			right: 0;
		}

		.btn-box {
			padding: 0 85rpx;
			margin-top: 20rpx;

			.login-button {
				// background: linear-gradient(90deg, #66DDFA 0%, #15A7F4 100%);
				// width: 520rpx;
				height: 90rpx;
				background: #2DD1AC;
				border-radius: 120rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-weight: 800;
				font-size: 32rpx;
				color: #FFFFFF;
				margin-top: 154rpx;
			}
		}
	}
</style>